<?php

class Model_Table_Product extends Zend_Db_Table {

    protected $_name = 'product';
    protected $_primary = 'id';

    public function allItems($arrParam = null, $options = null) {
        $db = $this->getAdapter();
        $info = new Zendvn_System_Info();
        $username = $info->getMemberInfo('username');
        $group_id = $info->getMemberInfo('group_id');
        
        $select = $db->select()->from('product AS p')->joinLeft('category AS c', 'p.cat_id = c.id', 'name AS CateName')->order('id DESC');
        if($group_id == 3 || $group_id == 4) $select->where('p.created_by = "' . $username . '"');
        if ($options == 'limit') {
            $select->limit($arrParam['limit'], $arrParam['offset']);
        } else if ($options == 'cate') {
            $select->where('p.cat_id =' . $arrParam['cat_id']);            
        }
        $result = $db->fetchAll($select);
        return $result;
    }
    
    public function countAllItems(){
        $sql = $this->select()->from($this, 'COUNT(*) AS Total');
        return $this->fetchRow($sql)->Total; 
    }

    public function getItem($arrParam = null, $options = null) {
        $db = $this->getAdapter();
        $select = $db->select()->from('product as p')->where('p.id =' . $arrParam['id']);
        return $db->fetchRow($select);
    }

    public function getItemsInCategory($arrParam = null, $options = null) {
        $db = $this->getAdapter();
        $select = $db->select()->from('product')->where('cat_id =' . $arrParam['cat_id'])->where('status = 1');
        return $db->fetchAll($select);
    }

    public function addItem($arrParam = null, $options = null) {
        $info = new Zendvn_System_Info();
        $username = $info->getMemberInfo('username');
        
        $row = $this->fetchNew();
        $row->name = $arrParam['name'];
        $row->picture = $arrParam['picture'];
        $row->price = $arrParam['price'];
        $row->selloff = $arrParam['selloff'];
        $row->warranty = $arrParam['warranty'];
        $row->promotion = $arrParam['promotion'];
        $row->intro = $arrParam['intro'];
        $row->detail = $arrParam['detail'];
        $row->created = time();
        $row->created_by = $username;
        $row->status = $arrParam['status'];
        $row->order = $arrParam['order'];
        $row->cat_id = $arrParam['cat_id'];
        $row->save();
    }

    public function deleteItem($arrParam = null, $options = null) {
        $db = $this->getAdapter();
        if ($options == 'multy') {
            $cid[] = $arrParam['cid'];
            $ids = $cid[0];
            foreach ($ids as $val) {
                $this->deleteOneItem($val);
            }
        } else {
            $this->deleteOneItem($arrParam['id']);
        }
    }

    public function deleteOneItem($id) {
        $info = new Zendvn_System_Info();
        $username = $info->getMemberInfo('username');
        
        $ItemsDetail = $this->getItem($id);
        if($ItemsDetail['created_by'] == $username){
            $where = 'id =' . $id;
            $db = $this->getAdapter();
            $select = $db->select()->from('product')->where('id =' . $id);
            $item = $db->fetchRow($select);
            $file = new Zendvn_File_Upload();
            $file->remove(PUBLIC_PATH . '/img/product/', $item['picture']);
            $this->delete($where);
        }else{
            header("Location: " . $_SERVER['HTTP_REFERER']);
        }
    }

    public function editItem($arrParam = null, $options = null) {
        $info = new Zendvn_System_Info();
        $username = $info->getMemberInfo('username');

        $where = 'id =' . (int) $arrParam['id'];
        $row = $this->fetchRow($where);
        $row->name = $arrParam['name'];
        $row->picture = $arrParam['picture'];
        $row->price = $arrParam['price'];
        $row->selloff = $arrParam['selloff'];
        $row->warranty = $arrParam['warranty'];
        $row->promotion = $arrParam['promotion'];
        $row->intro = $arrParam['intro'];
        $row->detail = $arrParam['detail'];
        $row->modified = time();
        $row->modified_by = $username;
        $row->status = $arrParam['status'];
        $row->order = $arrParam['order'];
        $row->cat_id = $arrParam['cat_id'];
        $row->save();
    }

    public function changeStatus($arrParam = null, $options = null) {
        $status = $arrParam['status'];
        $where = 'id =' . (int) $arrParam['id'];
        $row = $this->fetchRow($where);
        if ($status == 1)
            $status = 0;
        else if ($status == 0)
            $status = 1;
        $row->status = $status;
        $row->save();
        return $status;
    }

}